package exception;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AimException {

	 /**
	  * 通过xml配置异常处理类 使用SimpleMappingExceptionResolver
	  * 引发的异常为：ArrayIndexOutOfBoundsException
	  */
	@RequestMapping("/testSimpleMappingExceptionResolver")
	public String testSimpleMappingExceptionResolver(@RequestParam("i") Integer i) {
		Integer [] x=new Integer[6];
		x[i]=44;
		return "success";
	}
	
	
	/**
	 * 导致抛出自定义异常
	 * @param i
	 * @return String
	 */
	@RequestMapping("/testNumberNotMatcherException")
	public String testNumberNotMatcherException(@RequestParam("i") Integer i) {
		if(i>10) {
			throw new NumberNotMatcherException();
		}
		return "success";
	}
		
	
	
	/**
	 * 处理数学异常类 ArithmeticException
	 * @return ModelAndView
	 * 注意几点：
	 * 1：入参不能带有Map
	 * 2：返回值必须是ModelAndView
	 * 3：方法参数中可以加入Exception 通过参数获取异常的详细信息
	 * 4：方法的优先级, 规则是:匹配与异常类型相同的类型,找不到 再向上找
	 * 5：在当前handler 找不到@ExceptionHandler 则将去 @ControllerAdvice 标注的类中寻找,需配置全局异常类
	 */
	@ExceptionHandler(value= {ArithmeticException.class})
	public ModelAndView handlerArithmeticException(Exception exception) {
		System.out.println(">>>>>>处理数学异常<<<<<<<");
		ModelAndView moAndView=new ModelAndView();
		moAndView.addObject("errorinfo", exception.getMessage());
		moAndView.setViewName("error");
		return moAndView;
	}
		
	
	
	/**
	 * 导致抛出数学异常
	 * @param i
	 * @return String
	 */
	@RequestMapping("/testException")
	public String testException(@RequestParam("i") Integer i) {
		System.out.println("结果："+5/i);
		return "success";
	}
		
		
}
